package com.muyu.vehicle.process;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.domain.vo.VehicleFenceVo;
import com.muyu.vehicle.service.IVehicleInfoService;
import com.vehicle.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author 牧鱼
 * @Classname ProcessVehicleOnLineThread
 * @Description TODO
 * @Date 2021/9/15
 */
public class SynchronizeVehicleFenceThread implements Runnable{

    private static final Logger log = LoggerFactory.getLogger(SynchronizeVehicleFenceThread.class);

    /**
     * redis
     */
    private final RedisTemplate<String,String> redisTemplate;

    /**
     * 车辆业务
     */
    private final IVehicleInfoService vehicleInfoService;
    /**
     * 需同步的VIN
     */
    private final String vin;

    public SynchronizeVehicleFenceThread(RedisTemplate<String, String> redisTemplate, IVehicleInfoService vehicleInfoService, String vin) {
        this.redisTemplate = redisTemplate;
        this.vehicleInfoService = vehicleInfoService;
        this.vin = vin;
    }

    @Override
    public void run() {
        List<VehicleFenceVo> vehicleFenceVoList = vehicleInfoService.selectVehicleFence(vin);
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        vehicleFenceVoList.forEach( vehicleFenceVo -> {
            if (StringUtils.isNotEmpty(vehicleFenceVo.getPath())){
                hashOperations.put(Config.VEHICLE_FENCE_BIND , vehicleFenceVo.getVin() , vehicleFenceVo.getPath());
            }
        });
    }
}
